草庐IT

string - 用 JRE 库替换 StrSubstitutor

全部标签

ruby - 搜索并替换为 ruby​​ 正则表达式

我在包含HTML的MySQL列中有一个文本blob字段。我必须更改一些标记,所以我想我会在ruby​​脚本中完成。Ruby在这里无关紧要,但很高兴看到它的答案。标记如下所示:foobarmeow我只需要更改第一个foo每个文本block到something_else同时保留字符串的其余部分。似乎无法使用Ruby获得正确的PCRE正则表达式。 最佳答案 #Theregexliteralsyntaxusing%r{...}allows/inyourregexwithoutescapingnew_str=my_str.sub(%r{[^}

ruby - 类型错误 : no implicit conversion of Hash into String

尝试解析一些JSON,为什么text是空的?期望的输出:text应该返回Helloworld\n\nApple,Harbor\n\nBanana,Kitchen\n\nMango,Bedroomtext="Helloworld"json='{"fruits":[{"name":"Apple","location":"Harbor"},{"name":"Banana","location":"Kitchen"},{"name":"Mango","location":"Bedroom"}]}'fruits=JSON.parse(json)defformat_fruits(fruits)fr

ruby - 如何替换 Ruby 中的 for 循环?

在Ruby中,使用for循环是一种糟糕的风格。这是普遍理解的。向我推荐的风格指南:(https://github.com/bbatsov/ruby-style-guide#source-code-layout)说:“永远不要使用for,除非你知道确切的原因。大多数时候应该使用迭代器。for是根据each实现的(因此你添加了一个间接级别),但有一个扭曲-for不会引入新的作用域(与each不同),并且在其block中定义的变量将在其外部可见。”给出的例子是:arr=[1,2,3]#badforeleminarrdoputselemend#goodarr.each{|elem|putsel

ruby - 如何在不转换为不同编码的情况下替换 Ruby 中的 UTF-8 错误?

为了将字符串转换为UTF-8并替换所有编码错误,您可以这样做:str.encode('utf-8',:invalid=>:replace)唯一的问题是如果str已经是UTF-8则它不起作用,在这种情况下仍然存在任何错误:irb>x="foo\x92bar".encode('utf-8',:invalid=>:replace)=>"foo\x92bar"irb>x.valid_encoding?=>false引用RubyDocs:Pleasenotethatconversionfromanencodingenctothesameencodingencisano-op,i.e.therec

ruby - Codecademy "converting between symbols and strings" ruby 类(class)

这些是Codecademy的说明:Wehaveanarrayofstringswe'dliketolateruseashashkeys,butwe'drathertheybesymbols.Createanewarray,symbols.Use.eachtoiterateoverthestringsarrayandconverteachstringtoasymbol,addingthosesymbolstosymbols.这是我写的代码(提供了strings数组):strings=["HTML","CSS","JavaScript","Python","Ruby"]symbols=[]

ruby - 在 Ruby 中,String() 和 #to_s 有什么区别

String(1.1)==(1.1).to_s=>trueString(1.1)===(1.1).to_s=>true这两种强制方式有区别吗?如果可以,你能演示一下吗? 最佳答案 docsfortheStringmethod说:ConvertsargtoaStringbycallingitsto_smethod.所以通常它们是相同的,但也有一些差异——尽管您不太可能真正看到它们。String()检查其参数的类,如果它还不是String,则对其调用to_s。直接调用to_s意味着无论如何都会调用该方法。考虑类:classMyStrin

ruby-on-rails - 为什么 String#gsub 的内容加倍?

s="#main='quotes's.gsub"'","\\'"#=>"#main=quotes'quotes"这似乎是错误的,我希望得到"#main=\\'quotes\\'"当我不使用转义字符时,它会按预期工作。s.gsub"'","*"#=>"#main=*quotes*"所以一定有转义的事情。使用ruby1.9.2p290我需要用反斜杠和引号替换单引号。更多的不一致:"\\'".length#=>2"\\*".length#=>2#Asexpected"'".gsub("'","\\*").length#=>2"'a'".gsub("'","\\*")#=>"\\*a\\*"(

ruby - 是否有 Ruby string#blank?方法?

String#blank?非常有用,但存在于Rails中,而不存在于Ruby中。Ruby中是否有类似的东西来替换:str.nil?||str.empty? 最佳答案 据我所知,普通Ruby中没有这样的东西。您可以像这样创建自己的:classNilClassdefblank?trueendendclassStringdefblank?self.strip.empty?endend这将适用于nil.blank?和a_string.blank?您可以将其扩展(就像rails一样)用于true/false和一般对象:classFalseCl

ruby - 什么是枚举器对象? (使用 String#gsub 创建)

我有一个属性数组如下,attributes=["test,2011","photo","198.1x198.1cm","Photo:ManuPKFullScreen"]当我这样做时,artist=attributes[-1].gsub("Photo:")partist我在终端中得到以下输出#想知道为什么我得到一个枚举器对象作为输出?提前致谢。编辑:请注意,我做的不是attributes[-1].gsub("Photo:",""),而是attributes[-1].gsub("Photo:")所以想知道为什么枚举器对象返回到这里(我期待一条错误消息)以及发生了什么。?ruby-1.9.2

ruby-on-rails - `merge' :string <%= form_for %> helper 的未定义方法 '####'

我有一个表单,在发布时呈现另一个表单。我想做的是将参数从第一种形式传递到第二种形式的某些隐藏字段中。第二种形式是使用form_for形式助手,而我试图做的是让它接受传递给它的参数。表单如下所示:"btnbtn-largebtn-success"%>当我做类似的事情时这个Action给我错误:NoMethodErrorinFind_numbers#createShowingC:/Sites/dentist/app/views/phones/new.html.erbwhereline#17raised:undefinedmethod`merge'for"1231231234":String